//InnerAudioContext实例
const app = getApp()
//InnerAudioContext实例
var audioCxt;
//动画
var audioAnimation;
audioCxt = wx.createInnerAudioContext();
audioCxt.src = 'https://zhimi-lake.oss-cn-beijing.aliyuncs.com/audio/kata.mp3';
 
Page({
  data: {
    scale: 20,
    isok:'',//2413
    num:0,
    play2:0,
    play3:0,
    play4:0,
    ok1:0,
    ok2:0,
    ok3:0,
    ok4:0,
    animationMain:null,
    animationMainInit:null,
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
    //在播放状态，绑定播放进度更新事件。然后控制进度条和时间显示
    audioCxt.onPlay((res) =>{ 
      //audioCxt.onTimeUpdate(this.timeUpdate)
      console.log('播放start')
    }),
    //音乐播放结束触发
    audioCxt.onEnded((res)=>{
      audioCxt.seek(0);
      console.log('播放end')
    })
  },
 
  tap1(e) {
    console.log('x==='+e.detail.x+' y==='+e.detail.y +' source==='+ e.detail.source);
    let x1 = e.detail.x;
    let y1 = e.detail.y;
    let x1ok = this.data.x1ok;
    let y1ok = this.data.y1ok;
    let scale = this.data.scale;
    //console.log(e.target.dataset.play)
    
    if((x1 >= x1ok-scale && x1 <= x1ok+scale) && (y1 >= y1ok-scale && y1 <= y1ok+scale))
    {
      let isok = this.data.isok+1+',';
      console.log('lock1 ok')
      wx.vibrateLong();
      this.setData({
        x1: e.detail.x,
        y1: e.detail.y,
        isok:isok,
        ok1:1,
      })
    }
    if(this.data.play1 == 0){
      this.playMusic();
    }
    this.setData({
      play1:1,
    })
    
  },
  
  selit(e) {
    console.log('num==='+e.target.dataset.num);
    let num = e.target.dataset.num;
    
    let scale = this.data.scale;

    // 心跳的外框动画  
    this.animationMain1= wx.createAnimation({  
      duration:100,    // 以毫秒为单位  
      timingFunction: 'linear',  
      delay: 0,  
      transformOrigin: '50% 50%',  
      success: function (res) {  
      }  
    });
    this.animationMain1.scale(1.15).step();
    this.setData({  
      animationMain: this.animationMain1.export(),  //输出动画
      animationMainInit:null,
    });  
    
    
    if(this.data.play4 == 0){
      //this.playMusic();
    }
    let isok = this.data.isok+num+',';
      console.log('isok==='+isok)
    this.setData({
      num:num,
      isok:isok,
    })
    
  },
  resetKey(){
    this.setData({
      isok:'',
      play1:0,
    play2:0,
    play3:0,
    play4:0,
    })
    wx.redirectTo({
      url: '../lock/test4',
    })
  },
  onOk(){
    let isok = this.data.isok;
    let num = this.data.num;
    var words = isok.split(",");
    words = this.uniqueStr(words);
    isok = words.join(',');
    let tip = '再想想!';

    // 心跳的外框动画  
    var animationMain2= wx.createAnimation({  
      duration:1000,    // 以毫秒为单位  
      timingFunction: 'linear',  
      delay: 100,  
      transformOrigin: '50% 50%',  
      success: function (res) {  
      }  
    });
    animationMain2.scale(1).step();

    if(isok == '8,7,5,13,'){
      console.log('锁开了!')
      tip = '锁开了!';
      wx.showModal({
        title: '提示',
        content: tip,
        success: (res) => {
          wx.redirectTo({
            url: '/pagesG/pages/gameinfo/playSelect?id=19'  //传递得游戏id,
          })            
        }
      })
    }
    else{
      console.log('再想想!'+ isok)
      this.setData({  
        num:0,
        animationMainInit: animationMain2,  //输出动画
        isok:'',
      }); 
      wx.showModal({
        title: '提示',
        content: tip,
        success: (res) => {
          // wx.redirectTo({
          //   url: '/pagesG/pages/gameinfo/playSelect?id=19'  //传递得游戏id,
          // })            
        }
      })
    }
    
  },
    //播放按钮事件
    playMusic : function(){
      audioCxt.play();
    },
  uniqueStr (array){
    return array.filter(function(el, index, arr) {
    return index == arr.indexOf(el);
    });
  },

})